<template>
<!--  <a-card :bordered="false" style="margin-bottom: 10px">-->
<!--    <a-form ref="searchFormRef" :model="searchFormState" layout="inline">-->
<!--      <a-form-item label="姓名" name="name">-->
<!--        <a-input v-model:value="searchFormState.name" placeholder="请输入组织名称关键词" style="width: 240px;" />-->
<!--      </a-form-item>-->
<!--			<a-form-item label="电话" name="phone">-->
<!--			  <a-input v-model:value="searchFormState.phone" placeholder="请输入组织名称关键词" style="width: 240px;" />-->
<!--			</a-form-item>-->
<!--			<a-form-item label="状态" name="status">-->
<!--			 <a-radio-group v-model:value="searchFormState.status">-->
<!--					<a-radio-button value="ENABLE">启用</a-radio-button>-->
<!--					<a-radio-button value="DISENABLED">禁用</a-radio-button>-->
<!--				</a-radio-group>-->
<!--			</a-form-item>-->
<!--			<a-form-item>-->
<!--				<a-space>-->
<!--					<a-button type="primary" @click="tableRef.refresh(true)">-->
<!--						<template #icon><SearchOutlined /></template>-->
<!--						查询-->
<!--					</a-button>-->
<!--					<a-button class="snowy-buttom-left" @click="reset">-->
<!--						<template #icon><redo-outlined /></template>-->
<!--						重置-->
<!--					</a-button>-->
<!--				</a-space>-->
<!--			</a-form-item>-->
<!--    </a-form>-->
<!--  </a-card>-->
	<a-button type="primary" @click="formRef.onOpen(undefined, 0)" style="width: 100px">
		增加一级
	</a-button>
		  <a-card :bordered="false">
		<!--    <s-table-->
<!--      ref="tableRef"-->
<!--      :alert="options.alert.show"-->
<!--      :columns="columns"-->
<!--      :data="loadData"-->
<!--      :expand-row-by-click="true"-->
<!--      :row-key="(record) => record.id"-->
<!--      :row-selection="options.rowSelection"-->
<!--      bordered-->
<!--    ><template #operator class="table-operator">-->
<!--		<a-space>-->
<!--			<a-button type="primary" @click="formRef.onOpen(undefined, 0)">-->
<!--				<template #icon><plus-outlined /></template>-->
<!--				<span>{{ $t('common.addButton') }}{{ $t('model.user') }}</span>-->
<!--			</a-button>-->
<!--		</a-space>-->
<!--	</template>-->
<!--      <template #bodyCell="{ column, record }">-->
<!--        <template v-if="column.dataIndex === 'avatar'">-->
<!--          <img :src="record.avatar" style="width: 40px;height: 40px;border-radius: 4px;" />-->
<!--        </template>-->
<!--				<template v-if="column.dataIndex === 'status'">-->
<!--					<a-switch :checked="record.status === 'ENABLE'" :loading="loading" @change="editStatus(record)" />-->
<!--				</template>-->
<!--        <template v-if="column.dataIndex === 'action'">-->
<!--			<a @click="formRef.onOpen(undefined,record.id)">{{ $t('common.addButton') }}</a>-->
<!--			<a-divider type="vertical" />-->
<!--			<a @click="formRef.onOpen(record)">{{ $t('common.editButton') }}</a>-->
<!--			<a-divider type="vertical" />-->
<!--			<a-popconfirm :title="$t('user.popconfirmDeleteUser')" placement="topRight" @confirm="removeUser(record)">-->
<!--				<a-button danger size="small" type="link">-->
<!--					{{ $t('common.removeButton') }}-->
<!--				</a-button>-->
<!--			</a-popconfirm>-->
<!--        </template>-->
<!--      </template>-->
<!--    </s-table>-->
	  <a-table  ref="tableRef" :columns="columns" :data-source="record" :defaultExpandAllRows="true">
		<template #bodyCell="{ column, record }">
		  <template v-if="column.dataIndex === 'avatar'">
			<img :src="record.avatar" style="width: 40px;height: 40px;border-radius: 4px;" />
		  </template>
				<template v-if="column.dataIndex === 'status'">
					<a-switch :checked="record.status === 'ENABLE'" :loading="loading" @change="editStatus(record)" />
				</template>
		  <template v-if="column.dataIndex === 'action'">
			<a @click="formRef.onOpen(undefined,record.id)">{{ $t('common.addButton') }}</a>
			<a-divider type="vertical" />
			<a @click="formRef.onOpen(record)">{{ $t('common.editButton') }}</a>
			<a-divider type="vertical" />
			<a-popconfirm :title="$t('user.popconfirmDeleteUser')" placement="topRight" @confirm="removeUser(record)">
				<a-button danger size="small" type="link">
					{{ $t('common.removeButton') }}
				</a-button>
			</a-popconfirm>
			  <a-divider type="vertical" />
			  <a @click="createQrCode(record)">邀请码</a>
		  </template>
		</template>
	  </a-table>
	  <Form ref="formRef" @successful="loadData({})" />
			  <a-modal v-model:visible="showImg" title="邀请码">
				<div style="width: 100%;display: flex;justify-content: center;align-items: center;">
					<img :src="ImgData" width="100%" height="100%" v-if="ImgData" style="height: 200px;width: 200px;">
					<a-spin v-else/>
				</div>
			  </a-modal>
  </a-card>

</template>

<script setup>
	import fzUserApi from '@/api/biz/fzRoleApi'
	import Form from './form.vue'
	const columns = [
		{
			title: '角色名称',
			dataIndex: 'roleName',
			width: 100
		},
		{
			title: '角色值',
			dataIndex: 'roleValue',
			width: 150
		},
		{
			title: '分账类型',
			dataIndex: 'fzType',
			width: 100
		},
		{
			title: '分账值',
			dataIndex: 'fzValue',
			width: 200
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '200px'
		}
	]
	const selectedRowKeys = ref([])

	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	// 定义tableDOM
	const loading = ref(false)
	const tableRef = ref(null)
	const formRef = ref()
	const searchFormRef = ref()
	const searchFormState = ref({})
	const record=ref([])
	function arrayToTree(data, parentId = null) {
		const tree = [];
		// 遍历数组
		for (let i=0;i<data.length;i++) {
			if (data[i].parentId == parentId) {

				// 递归查找子节点
				const children = arrayToTree(data, data[i].id);
				if (children.length > 0) {
					data[i].children = children;
				}
				tree.push(data[i]);
			}
		}
		return tree;
	}


	// 表格查询 返回 Promise 对象
	const loadData = (parameter) => {
		fzUserApi.page(Object.assign(parameter, searchFormState.value)).then((res) => {
			console.log(res);
			record.value=arrayToTree(res?.records,0)
			console.log(record.value)
		})


	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 修改状态
	const editStatus = (record) => {
		let param = Object.assign({},record)
		param.status = record.status=='ENABLE'?'DISABLED':'ENABLE'
		loading.value = true
		fzUserApi
			.submitForm(param,param.id)
			.then(() => {
				tableRef.value.refresh()
			})
			.finally(() => {
				loading.value = false
			})
	}
	// 删除用户
	const removeUser = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		fzUserApi.remove(params).then(() => {
			tableRef.value.refresh()
		})
	}
	// 重置密码
	const restPwd = (record) => {
		let param = Object.assign({},record)
		param.password = "123456"
		loading.value = true
		fzUserApi
			.submitForm(param,param.id)
			.then(() => {
				tableRef.value.refresh()
			})
			.finally(() => {
				loading.value = false
			})
	}
	const showImg=ref(false)
	const ImgData=ref(null)
	const createQrCode = (record) => {
		showImg.value=true
		let json={
				page: 'pages/login/register2',
				scene: record.roleValue,
				envVersion: "release",
				checkPath: false,
				type: "fz",
			}
		fzUserApi.generateWxaQrCode(json).then((res) => {
			ImgData.value=res
		})
	}
	loadData({})
</script>

<style scoped>
	.ant-form-item {
		margin-bottom: 0 !important;
	}
	.primaryAdd {
		margin-right: 10px;
	}
	.snowy-buttom-left {
		margin-left: 8px;
	}
</style>
